import { PageIn } from '@idmy/core'

export function pageInvoice(pageIn: PageIn) {
  return http.post('/api/inv/invoice/page', pageIn)
}
export function findInvoiceLinesByMainId({ mainId }) {
  return http.post<any[]>('/api/inv/invoice-line/findByMainId', { mainId })
}

export const retrieve = id => http.post('/api/inv/invoice-print/retrieve', { id })

export const getPreviewById = id => http.post('/api/inv/invoice/getPreviewById', { id })

export const printBefore = async id => {
  const tmp = await http.post<any[]>('/api/inv/invoice-print/printBefore', { id })
  const papers = [tmp]
  const params = []
  for (const inv of papers) {
    const lines = []
    for (const item of inv.lines) {
      lines.push({
        amount: item.amt,
        itemName: item.name,
        itemNo: item.code,
        itemSpec: item.spec,
        itemUnit: item.unit || '',
        price: item.price,
        qty: item.qty,
      })
    }
    params.push({
      gender: inv.gender === 'MAN' ? '男' : '女',
      idCertNo: inv.cardNo,
      name: inv.name,
      cashier: inv.payee,
      outpatientNo: inv.outpatient.no || '',
      amount: inv.amt,
      invAt: inv.ticketAt,
      invCode: inv.ticketCode,
      invNo: inv.ticketNo,
      lines,
    })
  }

  return {
    templateId: 1,
    params,
  }
}

export const printSuccess = id => http.post('/api/inv/invoice-print/printSuccess', { id })

export const printFail = id => http.post('/api/inv/invoice-print/printFail', { id })

export const updateStatus = (id, printStatus) => http.post('/api/inv/invoice-print/updatePrintStatus', { id, printStatus })
